﻿//-----------------------------------------------------------------------
// <copyright file="ApplicationController.cs" company="OOHM PROCESSAMENTO DE DADOS LTDA.">
//     Microsoft Public License (MS-PL) This license governs use of the accompanying
//     software. If you use the software, you accept this license. If you do not
//     accept the license, do not use the software. 1. Definitions The terms "reproduce,"
//     "reproduction," "derivative works," and "distribution" have the same meaning
//     here as under U.S. copyright law. A "contribution" is the original software,
//     or any additions or changes to the software. A "contributor" is any person
//     that distributes its contribution under this license. "Licensed patents"
//     are a contributor's patent claims that read directly on its contribution.
//     2. Grant of Rights (A) Copyright Grant- Subject to the terms of this license,
//     including the license conditions and limitations in section 3, each contributor
//     grants you a non-exclusive, worldwide, royalty-free copyright license to
//     reproduce its contribution, prepare derivative works of its contribution,
//     and distribute its contribution or any derivative works that you create.
//     (B) Patent Grant- Subject to the terms of this license, including the license
//     conditions and limitations in section 3, each contributor grants you a non-exclusive,
//     worldwide, royalty-free license under its licensed patents to make, have
//     made, use, sell, offer for sale, import, and/or otherwise dispose of its
//     contribution in the software or derivative works of the contribution in
//     the software. 3. Conditions and Limitations (A) No Trademark License- This
//     license does not grant you rights to use any contributors' name, logo, or
//     trademarks. (B) If you bring a patent claim against any contributor over
//     patents that you claim are infringed by the software, your patent license
//     from such contributor to the software ends automatically. (C) If you distribute
//     any portion of the software, you must retain all copyright, patent, trademark,
//     and attribution notices that are present in the software. (D) If you distribute
//     any portion of the software in source code form, you may do so only under
//     this license by including a complete copy of this license with your distribution.
//     If you distribute any portion of the software in compiled or object code
//     form, you may only do so under a license that complies with this license.
//     (E) The software is licensed "as-is." You bear the risk of using it. The
//     contributors give no express warranties, guarantees or conditions. You may
//     have additional consumer rights under your local laws which this license
//     cannot change. To the extent permitted under your local laws, the contributors
//     exclude the implied warranties of merchantability, fitness for a particular
// </copyright>
//-----------------------------------------------------------------------
namespace AbstractSaaSTemplate.Application.Mvc{    using System.Web.Mvc;

    /// <summary>
    /// Provides methods that respond to HTTP requests that are made to an ASP.NET
    /// MVC Web site, customised for this application
    /// </summary>
    public class ApplicationController : Controller
    {
        /// <summary>
        /// Retrieves an Xml representation of the model.
        /// </summary>
        /// <param name="model">The model that is rendered by the view, in whichever format the method defines as correct based on the {format} route value.</param>
        /// <returns>Returns an instance of XmlResult with the representation of the model</returns>
        [System.Diagnostics.CodeAnalysis.SuppressMessage(
            "Microsoft.Performance",
            "CA1822:MarkMembersAsStatic",
            Justification = "We should keep this to keep the Mvc Pattern for result retrieval.")]
        protected ActionResult Xml(object model)
        {
            return new XmlResult(model);
        }

        /// <summary>
        /// Based on the parameter {format} of the request, it decides whether to return
        /// a ViewResult (and if it does, it returns with the name of the action invoked)
        /// so we can easily compare the ViewName in the unit testing environment, or it
        /// returns a JsonResult or XmlResult representing the model (in case the extension
        /// of the request is .json or .xml)
        /// </summary>
        /// <returns>The result which can be either an XmlResult, JsonResult or a ViewResult</returns>
        protected ActionResult Result()
        {
            return this.Result(null, null);
        }

        /// <summary>
        /// Based on the parameter {format} of the request, it decides whether to return
        /// a ViewResult (and if it does, it returns with the name of the action invoked)
        /// so we can easily compare the ViewName in the unit testing environment, or it
        /// returns a JsonResult or XmlResult representing the model (in case the extension
        /// of the request is .json or .xml)
        /// </summary>
        /// <param name="model">The model that is rendered by the view, in whichever format the method defines as correct based on the {format} route value.</param>
        /// <returns>The result which can be either an XmlResult, JsonResult or a ViewResult</returns>
        protected ActionResult Result(object model)
        {
            return this.Result(null, model);
        }
        
        /// <summary>
        /// Based on the parameter {format} of the request, it decides whether to return
        /// a ViewResult (and if it does, it returns with the name of the action invoked)
        /// so we can easily compare the ViewName in the unit testing environment, or it
        /// returns a JsonResult or XmlResult representing the model (in case the extension
        /// of the request is .json or .xml)
        /// </summary>
        /// <param name="viewName">The name of the view which will be retrieved</param>
        /// <param name="model">The model that is rendered by the view, in whichever format the method defines as correct based on the {format} route value.</param>
        /// <returns>The result which can be either an XmlResult, JsonResult or a ViewResult</returns>
        protected ActionResult Result(string viewName, object model)
        {
            // this is the key which will get from the route data
            // the parameter which indicates the format
            const string ActionKey = "action";
            const string FormatKey = "format";
            
            // now we attempt to get the format of the controller
            var format = this.ControllerContext.RouteData.Values[FormatKey] as string;

            // if the viewName is not defined, we try to load
            // the action from the route as defualt viewName
            // it's important to help unit testing to verify if
            // the right view is being returned
            if (string.IsNullOrWhiteSpace(viewName))
            {
                viewName = this.ControllerContext.RouteData.Values[ActionKey] as string;
            }
            
            // and finally we decide in which format we will return
            // the model passed on to us
            switch (format)
            {
                case "rss":
                case "xml":
                    return this.Xml(model);
                case "json":
                    return this.Json(model, JsonRequestBehavior.AllowGet);
                default:
                    return this.View(viewName, model);
            }
        }
    }
}

